// pages/index/order.js
var util = require('../../utils/util.js');
var app = getApp()
Page({
  data: {
    openid: '', //用户ID 测试ID：10086
    list: [],
    page: 1,
    pagesize: 1,
    hasMore: true,
    isHidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '订单'
    })
  },

  bindViewTap: function(e) {
    var that = this;
    wx.navigateTo({
      url: 'orderdetails?id=' + e.currentTarget.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //是否登录过  1是  0否
    if(util.user){
      that.userLogin();
    }else{
      //未登录
      app.login(function (user) {
        that.userLogin();
      }, function () {
        //跳转登录
        that.navigation(false);
      });
    }
  },

/**
 * 页面跳转逻辑
 */
userLogin:function(){
  var that = this;
  //是否绑手机  
  if (util.user.phone.length > 0) {
    //订单列表
    that.getOrderList();
  } else {
    //未绑手机
    if (util.user.ishome) {
      //跳转首页
      that.navigation(true);
    } else {
      //跳转登录
      that.navigation(false);
    }
  }
},

/**
 * 页面跳转
 */
  navigation:function(isRet){
    //isRet  false:登录   true:首页
    if (isRet){
      wx.switchTab({
        url: 'index'
      })
    }else{
      wx.navigateTo({
        url: 'login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    var that = this
    //是否跳转首页 true是  false否
    var userInfo = util.user;
    userInfo.ishome = false;
    util.user = userInfo;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    //加载更多
    var that = this;
    var more = that.data.hasMore;
    if (more) {
      // this.getMoreOrderList();
    } else {
      wx.showToast({
        title: '已加载全部数据',
        icon: 'none',
        duration: 500
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 订单列表
   */
  getOrderList: function() {
    var that = this;
    // wx.showLoading({
    //   title: '加载中'
    // });
    var openid = util.user.openid;
    that.setData({
      openid: openid
    })

    var url = util.URL.order_list + '?openid=' + openid + '&p=' + that.data.page + '&pagesize=' + that.data.pagesize;
    util.GET(url, function(sData) {
      wx.hideLoading();
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300
      })
      that.setData({
        list: sData.info,
        page: 2,
        isHidden:true
      })
      wx.stopPullDownRefresh();
    }, function(eData) {
      wx.hideLoading();
    });
  },

  //加载更多订单
  getMoreOrderList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中'
    });
    var url = util.URL.order_list + '?openid=' + that.data.openid + '&p=' + that.data.page + '&pagesize=' + that.data.pagesize;

    util.GET(url, function(sData) {
      wx.hideLoading();
      var more = true;
      var newLists = that.data.shoplist.concat(sData.info);
      if (sData.info.length <= 0) {
        more = false;
        wx.showToast({
          title: '已加载全部数据',
          icon: 'none',
          duration: 500
        })
      }
      that.setData({
        list: newLists,
        page: that.data.page + 1,
        hasMore: more
      })
    }, function(eData) {
      wx.hideLoading();
    });
  }
})